﻿using System;
using System.Collections;
using System.Linq;
using Pap.Data;
using Pap.Services;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class TodayPaymentsControlViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        ILoanExecutionService _LoanExecutionService;
        /// <summary>
        /// Initializes a new instance of the <see cref="TodayPaymentsControlViewModel"/> class.
        /// </summary>
        /// <param name="loanRepository">The loan repository.</param>
        /// <param name="loanExecutionService">The loan execution service.</param>
        public TodayPaymentsControlViewModel(ILoanRepository loanRepository, ILoanExecutionService loanExecutionService)
        {
            _LoanRepository = loanRepository;
            _LoanExecutionService = loanExecutionService;
        }

        /// <summary>
        /// Gets the today payments.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetTodayPayments()
        {
            var todaysPayments = _LoanRepository.FindLoanInstallmentPayments(loanInstPayment => loanInstPayment.PaymentDate.Date.Equals(DateTime.Today.Date));
            return (from payment in todaysPayments
                    select new
                    {
                        LoanNumber = payment.LoanExecutionInstallment.LoanExecution.CustomerLoan.LoanNumber,
                        CustomerName = payment.LoanExecutionInstallment.LoanExecution.CustomerLoan.CustomerObj.CustomerName,
                        PaymentAmount = payment.PaymentAmount,
                        RemainingAmount = _LoanExecutionService.GetAllExcuationDues(payment.LoanExecutionInstallment.LoanExecutionID)
                    });
        }
    }
}
